package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import project.taip.wawwe.model.Ingredient;



public class RecipeUtil {

	private static List<String> ingredientsFile = new ArrayList<String>();
	private static List<String> unitsFile = new ArrayList<String>();
	private static char[] signs = { ':', '!', '?', '.', ',' };

	static {
		File ingredients = new File("C:\\ingredients.txt");
		File units = new File("C:\\units.txt");

		int ch;
		StringBuffer strContent = new StringBuffer("");
		FileInputStream fin = null;

		try {

			fin = new FileInputStream(ingredients);

			while ((ch = fin.read()) != -1)
				strContent.append((char) ch);
			fin.close();

			String[] ingredientss = strContent.toString().split("\n");
			for (String s : ingredientss) {
				ingredientsFile.add(s);
			}
			
			strContent = new StringBuffer("");
			
			fin = new FileInputStream(units);

			while ((ch = fin.read()) != -1)
				strContent.append((char) ch);
			fin.close();

			String[] unitss = strContent.toString().split("\n");
			for (String s : unitss) {
				unitsFile.add(s);
			}

		} catch (FileNotFoundException e) {
			System.out.println("File " + ingredients.getAbsolutePath()
					+ " could not be found on filesystem");
		} catch (IOException ioe) {
			System.out.println("Exception while reading the file" + ioe);
		}
	}

	public static ArrayList<Ingredient> getIngredientsFromText(String text) {
		
		text = replaceSigns(text);
		
		String[] words = text.split(" ");
		
		ArrayList<Ingredient> ingredients = getIngredients(words);
		
		return ingredients;
	}

	private static ArrayList<Ingredient> getIngredients(String[] words) {
		int length = words.length;
		ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
		
		for (int i = 0 ; i < length ; i++) {
			if (i < length - 2) {
				if (isNumber(words[i])) {
					if (isUnit(words[i+1])) {
						if (isIngredient(words[i+2])) {
							Ingredient ing = new Ingredient();
							ing.setName(words[i+2]);
							ing.setQuantity(Integer.parseInt(words[i]));
							ing.setUnit(words[i+1]);
							ingredients.add(ing);
							i = i + 3;
						}
					}
				} else {
					if (isIngredient(words[i])) {
						Ingredient ing = new Ingredient();
						ing.setName(words[i]);
						ingredients.add(ing);
					}
				}
			} else {
				if (isIngredient(words[i])) {
					Ingredient ing = new Ingredient();
					ing.setName(words[i]);
					ingredients.add(ing);
				}
			}
		}
		
		return ingredients;
	}
	
	private static boolean isIngredient(String word) {
		word = word + "\r";
		return ingredientsFile.contains(word);
	}
	
	private static boolean isUnit(String word) {
		word = word + "\r";
		return unitsFile.contains(word);
	}
	
	private static boolean isNumber(String word) {
		try {
			Integer.parseInt(word);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	private static String replaceSigns(String text) {
		for (char s : signs) {
			try {
				text = text.replace(s,' ');
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return text;
	}

}
